import { Status } from './base';

// 配置项类型
export type ConfigType = 'string' | 'number' | 'boolean' | 'json' | 'array' | 'file';

// 配置项
export interface ConfigItem {
  id: string;
  key: string;
  name: string;
  value: any;
  defaultValue: any;
  type: ConfigType;
  category: string;
  description?: string;
  isSystem: boolean;
  isEncrypted: boolean;
  isRequired: boolean;
  validation?: {
    pattern?: string;
    min?: number;
    max?: number;
    options?: string[];
  };
  sort: number;
  scope: 'global' | 'tenant' | 'organization';
  tenantId?: string;
  organizationId?: string;
  createdAt: string;
  updatedAt: string;
  updatedBy?: string;
}

// 配置分类
export interface ConfigCategory {
  code: string;
  name: string;
  description?: string;
  icon?: string;
  sort: number;
  itemCount?: number;
}

// 系统设置
export interface SystemSettings {
  // 基础设置
  system: {
    siteName: string;
    siteUrl: string;
    adminEmail: string;
    timezone: string;
    language: string;
    dateFormat: string;
    timeFormat: string;
    logoUrl?: string;
    faviconUrl?: string;
  };
  
  // 安全设置
  security: {
    passwordMinLength: number;
    passwordRequireUppercase: boolean;
    passwordRequireLowercase: boolean;
    passwordRequireDigits: boolean;
    passwordRequireSpecialChars: boolean;
    passwordExpiryDays: number;
    maxFailedLoginAttempts: number;
    lockoutDurationMinutes: number;
    sessionTimeoutMinutes: number;
    twoFactorRequired: boolean;
    allowedDomains: string[];
  };
  
  // 邮件设置
  email: {
    enabled: boolean;
    provider: 'smtp' | 'ses' | 'sendgrid';
    smtpHost?: string;
    smtpPort?: number;
    smtpUsername?: string;
    smtpPassword?: string;
    smtpSecure: boolean;
    fromEmail: string;
    fromName: string;
    replyToEmail?: string;
  };
  
  // 存储设置
  storage: {
    provider: 'local' | 's3' | 'oss' | 'cos';
    localPath?: string;
    s3Bucket?: string;
    s3Region?: string;
    s3AccessKey?: string;
    s3SecretKey?: string;
    maxFileSize: number; // MB
    allowedExtensions: string[];
  };
  
  // 日志设置
  logging: {
    level: 'debug' | 'info' | 'warn' | 'error';
    retentionDays: number;
    enableApiLog: boolean;
    enableLoginLog: boolean;
    enableOperationLog: boolean;
    enableErrorLog: boolean;
  };
}

// 租户配置
export interface TenantConfig {
  tenantId: string;
  // 界面配置
  ui: {
    theme: string;
    primaryColor: string;
    secondaryColor: string;
    logo?: string;
    favicon?: string;
    customCss?: string;
  };
  
  // 功能配置
  features: {
    enableMultiOrganization: boolean;
    enableDepartment: boolean;
    enablePosition: boolean;
    enableWorkflow: boolean;
    enableReporting: boolean;
    enableApiAccess: boolean;
  };
  
  // 业务配置
  business: {
    workingDays: string[]; // ['Monday', 'Tuesday', ...]
    workingHours: {
      start: string; // '09:00'
      end: string; // '18:00'
    };
    holidays: string[]; // ['2024-01-01', '2024-02-10', ...]
    currency: string;
    language: string;
    timezone: string;
  };
}

// 组织配置
export interface OrganizationConfig {
  organizationId: string;
  // 组织特定设置
  workflow: {
    enableApproval: boolean;
    maxApprovalLevels: number;
    defaultApprover?: string;
  };
  
  reporting: {
    enableCustomReports: boolean;
    maxReportFields: number;
    allowDataExport: boolean;
  };
  
  integration: {
    enableSso: boolean;
    ssoProvider?: string;
    ssoConfig?: any;
    enableLdap: boolean;
    ldapConfig?: any;
  };
}

// 审计日志
export interface AuditLog {
  id: string;
  tenantId: string;
  organizationId?: string;
  userId: string;
  username: string;
  userIp: string;
  userAgent: string;
  action: string;
  resource: string;
  resourceId?: string;
  method: string;
  url: string;
  status: 'success' | 'failed';
  statusCode: number;
  errorMessage?: string;
  requestData?: any;
  responseData?: any;
  duration: number; // 毫秒
  createdAt: string;
}

// 登录日志
export interface LoginLog {
  id: string;
  tenantId: string;
  userId?: string;
  username: string;
  ip: string;
  userAgent: string;
  location?: string;
  status: 'success' | 'failed';
  failureReason?: string;
  createdAt: string;
}

// 操作日志
export interface OperationLog {
  id: string;
  tenantId: string;
  organizationId?: string;
  userId: string;
  username: string;
  module: string;
  operation: string;
  description: string;
  target?: string;
  targetId?: string;
  oldValue?: any;
  newValue?: any;
  ip: string;
  userAgent: string;
  createdAt: string;
}

// 系统监控
export interface SystemMonitor {
  timestamp: string;
  cpu: {
    usage: number; // 百分比
    cores: number;
  };
  memory: {
    total: number; // 字节
    used: number;
    usage: number; // 百分比
  };
  disk: {
    total: number;
    used: number;
    usage: number; // 百分比
  };
  network: {
    bytesReceived: number;
    bytesSent: number;
  };
  database: {
    connections: number;
    maxConnections: number;
    queryTime: number; // 平均查询时间，毫秒
  };
  redis?: {
    memory: number;
    connections: number;
    hitRate: number; // 命中率
  };
}

// 配置查询过滤器
export interface ConfigFilter {
  category?: string;
  scope?: 'global' | 'tenant' | 'organization';
  tenantId?: string;
  organizationId?: string;
  isSystem?: boolean;
  keyword?: string;
}

// 日志查询过滤器
export interface LogFilter {
  tenantId?: string;
  organizationId?: string;
  userId?: string;
  action?: string;
  resource?: string;
  status?: string;
  startDate?: string;
  endDate?: string;
  ip?: string;
  keyword?: string;
}